package com.iring.domain.pay;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.iring.domain.config.PaypalPaymentIntent;
import com.paypal.api.payments.Currency;
import com.paypal.api.payments.Links;
import com.paypal.api.payments.Metadata;
import com.paypal.api.payments.PayerInfo;
import com.paypal.api.payments.PaymentDetail;
import com.paypal.api.payments.RedirectUrls;
import com.paypal.base.rest.APIContext;
import com.paypal.base.rest.HttpMethod;
import com.paypal.base.rest.PayPalRESTException;
import com.paypal.base.rest.PayPalResource;
import com.paypal.base.rest.RESTUtil;

/**
 * paypal订单
 *
 * @author wanggan
 */
public class OrderPayment extends PayPalResource {
    private static final Logger logger = LoggerFactory.getLogger(OrderPayment.class);
    private String id;
    private PaypalPaymentIntent intent;
    private PaymentDetail paymentDetails;
    private Currency grossTotalAmount;
    private List<PurchaseUnits> purchaseUnits;
    private PayerInfo payerInfo;
    private Metadata metadata;
    private RedirectUrls redirectUrls;
    private String createTime;
    private String updateTime;
    private List<Links> links;
    private String status;



    public OrderPayment create(APIContext apiContext) throws PayPalRESTException {

//        String path = "/v1/risk/transaction-contexts";
        String payLoad = this.toJSON();

        String resourcePath = "/v1/checkout/orders";
        OrderPayment orderPayment = configureAndExecute(apiContext, HttpMethod.POST, resourcePath, payLoad, OrderPayment.class);
        apiContext.setRequestId(null);
        return orderPayment;
    }

    public OrderPayment get(APIContext apiContext,String orderid) throws PayPalRESTException {
       if (orderid == null) {
            throw new IllegalArgumentException("orderid cannot be null");
        } else {
            Object[] parameters = new Object[]{this.getId()};
            String pattern = "/v1/checkout/orders/{0}";
            String resourcePath = RESTUtil.formatURIPath(pattern, parameters);
            return configureAndExecute(apiContext, HttpMethod.GET, resourcePath, orderid, OrderPayment.class);
        }
    }

    public void delete(APIContext apiContext, String orderid) throws PayPalRESTException {
       if (orderid == null) {
            throw new IllegalArgumentException("orderid cannot be null");
        } else {
            Object[] parameters = new Object[]{this.getId()};
            String pattern = "/v1/checkout/orders/{0}";
            String resourcePath = RESTUtil.formatURIPath(pattern, parameters);
            PayPalResource.configureAndExecute(apiContext, HttpMethod.DELETE, resourcePath, orderid, null);
        }
    }

    public OrderPayment capture(APIContext apiContext,String orderid) throws PayPalRESTException {
        if (orderid == null) {
            throw new IllegalArgumentException("orderid cannot be null");
        } else {
            Object[] parameters = new Object[]{this.getId()};
            String pattern = "/v1/checkout/orders/{0}/capture";
            String resourcePath = RESTUtil.formatURIPath(pattern, parameters);
            return configureAndExecute(apiContext, HttpMethod.POST, resourcePath, orderid, OrderPayment.class);
        }
    }

    public String getId() {
        return id;
    }

    public Currency getGrossTotalAmount() {
        return grossTotalAmount;
    }

    public List<PurchaseUnits> getPurchaseUnits() {
        return purchaseUnits;
    }

    public PayerInfo getPayerInfo() {
        return payerInfo;
    }

    public Metadata getMetadata() {
        return metadata;
    }

    public RedirectUrls getRedirectUrls() {
        return redirectUrls;
    }

    public String getCreateTime() {
        return createTime;
    }

    public String getUpdateTime() {
        return updateTime;
    }

    public List<Links> getLinks() {
        return links;
    }

    public String getStatus() {
        return status;
    }

    public PaypalPaymentIntent getIntent() {
        return intent;
    }

    public PaymentDetail getPaymentDetails() {
        return paymentDetails;
    }



    public void setIntent(PaypalPaymentIntent intent) {
        this.intent = intent;
    }

    public void setPaymentDetails(PaymentDetail paymentDetails) {
        this.paymentDetails = paymentDetails;
    }


    public void setId(String id) {
        this.id = id;
    }

    public void setGrossTotalAmount(Currency grossTotalAmount) {
        this.grossTotalAmount = grossTotalAmount;
    }

    public void setPurchaseUnits(List<PurchaseUnits> purchaseUnits) {
        this.purchaseUnits = purchaseUnits;
    }

    public void setPayerInfo(PayerInfo payerInfo) {
        this.payerInfo = payerInfo;
    }

    public void setMetadata(Metadata metadata) {
        this.metadata = metadata;
    }

    public void setRedirectUrls(RedirectUrls redirectUrls) {
        this.redirectUrls = redirectUrls;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public void setUpdateTime(String updateTime) {
        this.updateTime = updateTime;
    }

    public void setLinks(List<Links> links) {
        this.links = links;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}
